var allBase = null;
var pGuid = null;
$(function () {
    $.when(
        $.get(baseURL+"csg/cpbase/select?ourid=isok",function (r) {
            allBase = r.returnlist;
            vm.allBase = allBase;
        })
    ).done(
		$("#jqGrid").jqGrid({
			//后台已有限制条件
			url: baseURL + 'csg/cstackpoint/list',
			datatype: "json",
			colModel: [
				{ label: 'guid', name: 'guid', index: 'guid', width: 50, key: true,hidden:true },
				{ label: '所属项目', name: 'pGuid', index: 'p_guid', width: 80,
					formatter: function (cellvalue, options, rowObject) {
						for (d in allBase) {
							if (cellvalue == allBase[d].guid) return allBase[d].pBName;
						}
					}
				},
				{ label: '类型(名称)', name: 'sPName', index: 's_p_name', width: 80 },
				{ label: '经度', name: 'sPLongitude', index: 's_p_longitude', width: 80 },
				{ label: '纬度', name: 'sPLatitude', index: 's_p_latitude', width: 80 },
				{ label: '状态', name: 'sPDatastate', index: 's_p_datastate', width: 80,hidden:true },
				{ label: '操作', name: '', width: 100,
					formatter: function (cellvalue, options, rowObject) {
						return "<a  class='btn btn-primary' onclick='showArrivalRecord(\"" + rowObject.guid + "\")' >&nbsp;物资清单</a>"
                            +  "&nbsp;&nbsp;<a  class='btn btn-primary' onclick='exports(\"" + rowObject.guid + "\")' >&nbsp;导出材料</a>"
                            +  "&nbsp;&nbsp;<a  class='btn btn-primary' onclick='exportsimg(\"" + rowObject.guid + "\")' >&nbsp;导出材料照片</a>"
					}
				}
			],
			viewrecords: true,
			height: 385,
			rowNum: 10,
			rowList : [10,30,50],
			rownumbers: true,
			rownumWidth: 25,
			autowidth:true,
			multiselect: true,
			pager: "#jqGridPager",
			jsonReader : {
				root: "page.list",
				page: "page.currPage",
				total: "page.totalPage",
				records: "page.totalCount"
			},
			prmNames : {
				page:"page",
				rows:"limit",
				order: "order"
			},
			gridComplete:function(){
				//隐藏grid底部滚动条
				$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
			}
		})
    )
});


function showArrivalRecord(guid) {
    var title ="物资清单";
    var pGuid = vm.pGuid;
    var content = "cmiddlegoodsdetail.html?guid=" + guid+"&pGuid="+pGuid;
    layer.open({
        type: 2,
        title: title,
        shadeClose: true,
        shade: 0.5,
        maxmin: true, //开启最大化最小化按钮
        area: ['95%', '95%'],
        content: content

    });
}
//根据堆放点导出材料
function exports(guid) {
    location.href=baseURL + "csg/cstackpoint/exports?guid="+guid;
}

function exportsimg(guid) {
    location.href=baseURL + "csg/cstackpoint/exportsimg?guid="+guid;
}


var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		pGuid:pGuid,
		allBase:allBase,
		cStackPoint: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.cStackPoint = {};
		},
		update: function (event) {
			var guid = getSelectedRow();
			if(guid == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(guid)
		},
		saveOrUpdate: function (event) {
			var pGuid = vm.pGuid;
			var url = vm.cStackPoint.guid == null ? "csg/cstackpoint/saveNew/"+pGuid : "csg/cstackpoint/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.cStackPoint),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var guids = getSelectedRows();
			if(guids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "csg/cstackpoint/delete",
                    contentType: "application/json",
				    data: JSON.stringify(guids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(guid){
			$.get(baseURL + "csg/cstackpoint/info/"+guid, function(r){
                vm.cStackPoint = r.cStackPoint;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				postData:{
					'pGuid':vm.pGuid
				},
                page:page
            }).trigger("reloadGrid");
		}
	}
});